package com.block.db.handle;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.time.LocalDateTime;

/**
 * 使用mybatis Plus 的自动填充功能，
 * 完成 创建时间 和 更新时间 的自动填充
 */
@Component
public class NemoMetaObjectHandler implements MetaObjectHandler {

    private static final String CREATE_TIME = "createTime";
    private static final String CREATE_DATE_TIME="createDatetime";
    private static final String CREATE_DATE = "createDate";

    private static final String UPDATE_TIME = "updateTime";
    private static final String UPDATE_DATE = "updateDate";
    private static final String UPDATE_DATETIME = "updateDatetime";
    private static final String LOGIN_DATE_TIME="loginDatetime";


    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = this.getFieldValByName(CREATE_TIME,metaObject);
        if (createTime == null){
            this.strictInsertFill(metaObject, CREATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }
        Object loginDatetime = this.getFieldValByName(LOGIN_DATE_TIME,metaObject);
        if (loginDatetime == null){
            this.strictInsertFill(metaObject, LOGIN_DATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }
        Object createDate = this.getFieldValByName(CREATE_DATE,metaObject);
        if (createDate == null){
            this.strictInsertFill(metaObject, CREATE_DATE, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }

        Object updateTime = this.getFieldValByName(UPDATE_TIME,metaObject);
        if (updateTime == null){
            this.strictInsertFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }
        Object updateDate = this.getFieldValByName(UPDATE_DATE,metaObject);
        if (updateDate == null){
            this.strictInsertFill(metaObject, UPDATE_DATE, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }
        Object createDateTime=this.getFieldValByName(CREATE_DATE_TIME,metaObject);
        if(createDateTime==null){
            this.strictInsertFill(metaObject, CREATE_DATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐使用)
        }
        Object updateDatetime = this.getFieldValByName(UPDATE_DATETIME,metaObject);
        if (updateDatetime == null){
            this.strictUpdateFill(metaObject, UPDATE_DATETIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = this.getFieldValByName(UPDATE_TIME,metaObject);
        if (updateTime == null){
            this.strictUpdateFill(metaObject, UPDATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        }
        Object loginDatetime = this.getFieldValByName(LOGIN_DATE_TIME,metaObject);
        if (loginDatetime == null){
            this.strictUpdateFill(metaObject, LOGIN_DATE_TIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        }
        Object updateDate = this.getFieldValByName(UPDATE_DATE,metaObject);
        if (updateDate == null){
            this.strictUpdateFill(metaObject, UPDATE_DATE, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        }
        Object updateDatetime = this.getFieldValByName(UPDATE_DATETIME,metaObject);
        if (updateDatetime == null){
            this.strictUpdateFill(metaObject, UPDATE_DATETIME, LocalDateTime.class, LocalDateTime.now()); // 起始版本 3.3.0(推荐)
        }
    }
}